package commons;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class JSonConvert {
	private List<String> user, device;
	private Context mContext;
	private String convertString;

	public JSonConvert(Context ctx, String strToConvert) {
		mContext = ctx;
		convertString = strToConvert;
		user = new ArrayList<String>();
		device = new ArrayList<String>();
	}

	public List<String> getUser() {
		JSONArray jaray = null;
		String userFound = "";
		try {
			jaray = new JSONArray(convertString);
			for (int i = 0; i < jaray.length(); i++) {
				userFound = jaray.getJSONObject(i).getString("user");
				if (!userFound.equals("null") && userFound != null) {
					user.add(userFound);
				}
			}
		} catch (JSONException e) {
			Toast.makeText(mContext, "@@problem @@: " + e.getMessage(), 4000).show();
			Log.e("data", e.getMessage());
			e.printStackTrace();
		}
//		if(user.size() > 0){
//			Toast.makeText(mContext, "found User", 1000).show();
//		}
		return user;
	}

	public List<String> getDevice() {
		JSONArray jaray = null;
		String deviceFound = "";
		try {
			jaray = new JSONArray(convertString);
			for (int i = 0; i < jaray.length(); i++) {
				deviceFound = jaray.getJSONObject(i).getString("device");
				if (!deviceFound.equals("null")) {
					device.add(deviceFound);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return device;
	}
}
